package com.hatic.process.task;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;
import org.jxstar.util.DateUtil;


public class CheckProcessBO extends BusinessObject{
	
	private static final long serialVersionUID = 5823424368129802602L;

	/**
	 * 检查日期 判断是否延期：
	 * 用当前日期与计划日期去比较，如果到了这个节点， 但台账中实际日期没填，也算延期;
	 * 从后面的那个日期字段开始比较， 如果项目台账中的哪个日期字段有值就开始比较，如果实际大于计划算延期；
	 * 
	 * 判断项目所处阶段：根据实际日期来判断，从后往前判断，如果没值就继续判断，有值就更新，返回；
	 * @return
	 */
	public String autoCheck(){
		String sql ="select * from v_process order by date_projectlz";      
		DaoParam param = _dao.createParam(sql); 		
		List<Map<String, String>> list = _dao.query(param); 
		
		String nowtime =DateUtil.getToday();
		
		if(list != null &&  list.size() >0) {
			for(int i=0;i<list.size();i++) {
				Map<String, String> map  =  list.get(i);
				String date_projectlz=map.get("date_projectlz");
				String date_ztb=map.get("date_ztb");
				String date_contract=map.get("date_contract");
				String date_projectend=map.get("date_projectend");
				String date_check=map.get("date_check");
				//String date_checkone=map.get("date_checkone");
				String date_moneycheck=map.get("date_moneycheck");
				String date_checkend=map.get("date_checkend");
				
				String date_lz=map.get("date_lz");
				String date_sjztb=map.get("date_sjztb");
				String date_ht=map.get("date_ht");
				String date_done=map.get("date_done");
				String date_yans=map.get("date_yans");
				String date_jues=map.get("date_jues");
				String date_sjjg=map.get("date_sjjg");
				
				String process_id=map.get("process_id");
				String project_id=map.get("project_id");
				String project_name=map.get("project_name");
				String is_defer=map.get("is_defer");
				String project_stage=map.get("project_stage");
				
				 _log.showDebug("序号："+i+"项目:"+project_name+","+project_id+"过程ID:"+process_id+"是否延期:"+is_defer+"项目阶段："+project_stage);
				 
				//更新项目阶段的值
				 if(date_checkend != null && date_checkend.length() !=0){
					 if(date_sjjg != null && date_sjjg.length() !=0 ){
						 updateStage(process_id,"80");
					 }
				 }else{
					 if(date_moneycheck != null && date_moneycheck.length() != 0){
						 if(date_jues != null && date_jues.length() !=0 ){
							 updateStage(process_id,"70");
						 }
					 }else{
						 if(date_check != null && date_check.length() != 0){
							 if(date_yans != null && date_yans.length() !=0 ){
								 updateStage(process_id,"60");
							 }
						 }else{
							 if(date_projectend != null && date_projectend.length() != 0){
								 if(date_done != null && date_done.length() !=0 ){
									 updateStage(process_id,"50");
								 }
							 }else{
								 if(date_contract != null && date_contract.length() != 0){
									 if(date_ht != null && date_ht.length() !=0 ){
										 updateStage(process_id,"40");
									 }
								 }else{
									 if(date_ztb != null && date_ztb.length() != 0){
										 if(date_sjztb != null && date_sjztb.length() !=0 ){
											 updateStage(process_id,"30");
										 }
									 }else{
										 if(date_projectlz != null && date_projectlz.length() != 0){
											 if(date_lz != null && date_lz.length() !=0 ){
												 updateStage(process_id,"20");
											 }else{
												 updateStage(process_id,"10");
											 } 
										 }else{
											 updateStage(process_id,"5");
										 } 
									 }
								 }
							 }
						 }
					 }
				 }
				 
				//从计划竣工日期开始判断
				//如果当前日期大于计划日期，判断实际日期是否有值，没有值则延期，实际日期大于计划日期则延期；否则是否延期值为“否”
				if(date_checkend != null && date_checkend.length() != 0){
					if(check(nowtime,date_checkend)){//当前日期大于计划日期
						if(date_sjjg == null || date_sjjg.length() ==0 ){//实际日期为空，则判定延期
							updateDefer(process_id,"1");
							continue;
						}else{
							if(check(date_sjjg,date_checkend)){//实际日期大于计划日期，则判定延期
								updateDefer(process_id,"1");
								continue;
							}else{
								updateDefer(process_id,"0");
								continue;
							}
						}
					}
				}
				
				
				//财务计算日期
				if(date_moneycheck != null && date_moneycheck.length() != 0){
					if(check(nowtime,date_moneycheck)){//当前日期大于计划日期
						if(date_jues == null || date_jues.length() ==0 ){//实际日期为空，则判定延期
							updateDefer(process_id,"1");
							continue;
						}else{				
							if(check(date_jues,date_moneycheck)){//实际日期大于计划日期，则判定延期
								updateDefer(process_id,"1");
								continue;
							}else{
								updateDefer(process_id,"0");
								continue;
							}
						}
					}
				}
				//内部验收
				if(date_check != null && date_check.length() != 0){
					if(check(nowtime,date_check)){//当前日期大于计划日期
						if(date_yans == null || date_yans.length() ==0 ){//实际日期为空，则判定延期
							updateDefer(process_id,"1");
							continue;
						}else{						
							if(check(date_yans,date_check)){//实际日期大于计划日期，则判定延期
								updateDefer(process_id,"1");
								continue;
							}else{
								updateDefer(process_id,"0");
								continue;
							}
						}
					}
				}
				//实施完成
				if(date_projectend != null && date_projectend.length() != 0){
					if(check(nowtime,date_projectend)){//当前日期大于计划日期
						if(date_done == null || date_done.length() ==0 ){//实际日期为空，则判定延期
							updateDefer(process_id,"1");
							continue;
						}else{
														
							if(check(date_done,date_projectend)){//实际日期大于计划日期，则判定延期
								updateDefer(process_id,"1");
								continue;
							}else{
								updateDefer(process_id,"0");
								continue;
							}
						}
					}
				}
				//合同签订
				if(date_contract != null && date_contract.length() != 0){
					if(check(nowtime,date_contract)){//当前日期大于计划日期
						if(date_ht == null || date_ht.length() ==0 ){//实际日期为空，则判定延期
							updateDefer(process_id,"1");
							continue;
						}else{
														
							if(check(date_ht,date_contract)){//实际日期大于计划日期，则判定延期
								updateDefer(process_id,"1");
								continue;
							}else{
								updateDefer(process_id,"0");
								continue;
							}
						}
					}
				}
				//招投标
				if(date_ztb != null && date_ztb.length() != 0){
					if(check(nowtime,date_ztb)){//当前日期大于计划日期
						if(date_sjztb == null || date_sjztb.length() ==0 ){//实际日期为空，则判定延期
							updateDefer(process_id,"1");
							continue;
						}else{
														
							if(check(date_sjztb,date_ztb)){//实际日期大于计划日期，则判定延期
								updateDefer(process_id,"1");
								continue;
							}else{
								updateDefer(process_id,"0");
								continue;
							}
						}
					}
				}
				//论证
				
				if(date_projectlz != null && date_projectlz.length() != 0){
					if(check(nowtime,date_projectlz)){//当前日期大于计划日期
						if(date_lz == null || date_lz.length() ==0 ){//实际日期为空，则判定延期
							updateDefer(process_id,"1");
							continue;
						}else{
							if(check(date_lz,date_projectlz)){//实际日期大于计划日期，则判定延期
								updateDefer(process_id,"1");
								continue;
							}else{
								updateDefer(process_id,"0");
								continue;
							}
						}
					}
				}
				else{
					updateDefer(process_id,"0");
					continue;
				}
			
			}
		}
		
		return _returnSuccess;
	}
	

	
	
	//判断time1是否大于time2，即time1在时间time2后面
	private boolean check(String time1, String time2) {
		// TODO Auto-generated method stub
		DateFormat df = new SimpleDateFormat("yyyy-mm");
		try{
			Date dt1=df.parse(time1);
			Date dt2=df.parse(time2);
			if(dt1.after(dt2)) {  
				System.out.println("time1:"+time1+"time2:"+time2+"========="+dt1+" 在"+dt2+"后=========="+"dt1.getTime："+dt1.getTime()+"dt2.getTime:"+dt2.getTime()); 
				return true;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}


/**
	//判断time1是否小于time2，即time1在时间time2前面
	private boolean check1(String time1, String time2) {
		// TODO Auto-generated method stub
		DateFormat df = new SimpleDateFormat("yyyy-mm");
		try{
			Date dt1=df.parse(time1);
			Date dt2=df.parse(time2);
			if( dt1.getTime() < dt2.getTime()){
				System.out.println(time1+" 在"+time2+"前"); 
				return true;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return false;
	}
*/

	//更新是否延期 的值
	private boolean updateDefer(String processId,String i) {
		String sql = "update project_process set is_defer = ?  where process_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(i);
		param.addStringValue(processId);
		
		return _dao.update(param);
	}
	
	//更新项目阶段的值
	private boolean updateStage(String processId,String i) {
		String sql = "update project_process set project_stage = ?  where process_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(i);
		param.addStringValue(processId);
		
		return _dao.update(param);
	}
	
}
